//
//  GuideViewController.m
//  钓鱼人
//
//  Created by 钓鱼人 on 16/6/12.
//  Copyright © 2016年 game. All rights reserved.
//

#import "GuideViewController.h"
#import "MainPageController.h"
#import "AppDelegate.h"
#import "TabBarController.h"

@interface GuideViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UIPageControl *page;
@property (nonatomic,strong) NSArray *imageArys;
@end

@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.imageArys = @[@"引导页1",@"引导页2",@"引导页3",@"引导页4"];
    
    _scrollview.delegate =self;
    self.scrollview.contentSize = CGSizeMake(SCREEN_WIDTH*(_imageArys.count), SCREEN_HEIGHT);
    for (int i=0; i<_imageArys.count; i++) {
        UIImageView *imageview = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.imageArys[i]]];
        imageview.frame = CGRectMake(SCREEN_WIDTH*i,0, SCREEN_WIDTH, SCREEN_HEIGHT);
        [self.scrollview addSubview:imageview];
        imageview.tag = i+10;
        
        //skip
        CGRect skiprect = CGRectMake((i+1)*screen_width - 70, 25, 45, 26);
        UIButton *passbtn = [[UIButton alloc] initWithFrame:skiprect];
        [passbtn addTarget:self action:@selector(LastImageviewTap) forControlEvents:(UIControlEventTouchUpInside)];
        passbtn.backgroundColor = [UIColor clearColor];
        [passbtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        NSString *title = i == _imageArys.count -1 ? @"进入" : @"跳过";
        [passbtn setTitle:title forState:(UIControlStateNormal)];
        [_scrollview addSubview:passbtn];
    }
    
    
    UIImageView *imageview = [self.view viewWithTag:_imageArys.count-1+10];
    imageview.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(LastImageviewTap)];
    [imageview addGestureRecognizer:tap];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.x >= _imageArys.count * screen_width) [self LastImageviewTap];
}

-(void)LastImageviewTap{
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    TabBarController *tab = [[TabBarController alloc]init];
    
    tab.modalTransitionStyle = UIModalPresentationCustom;
    
    [self presentViewController:tab animated:YES completion:^{
        app.myTabBar = tab;
        
        app.window.rootViewController = tab;
        [app.window makeKeyAndVisible];
        
    }];

}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    _page.currentPage = scrollView.contentOffset.x/SCREEN_WIDTH;
}

@end
